﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Hr.DAL
{
    /// <summary>
    /// 数据访问类Hr_Contacts。
    /// </summary>
    public class Hr_Contacts
    {
        public Hr_Contacts()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Hr_Contacts where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Hr.Entity.Hr_Contacts model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Hr_Contacts(");
            strSql.Append("RecordId,OwnerId,UserName,Company,Depart,Position,Email,Mobile,Tel,Remarks,Category,UpdateTime,Initial)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@OwnerId,@UserName,@Company,@Depart,@Position,@Email,@Mobile,@Tel,@Remarks,@Category,@UpdateTime,@Initial)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "OwnerId", DbType.Guid, model.OwnerId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "Company", DbType.AnsiString, model.Company);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "Position", DbType.AnsiString, model.Position);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Tel", DbType.AnsiString, model.Tel);
            db.AddInParameter(dbCommand, "Remarks", DbType.AnsiString, model.Remarks);
            db.AddInParameter(dbCommand, "Category", DbType.Int32, model.Category);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "Initial", DbType.AnsiString, model.Initial);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Hr.Entity.Hr_Contacts model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Hr_Contacts set ");
            strSql.Append("OwnerId=@OwnerId,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Company=@Company,");
            strSql.Append("Depart=@Depart,");
            strSql.Append("Position=@Position,");
            strSql.Append("Email=@Email,");
            strSql.Append("Mobile=@Mobile,");
            strSql.Append("Tel=@Tel,");
            strSql.Append("Remarks=@Remarks,");
            strSql.Append("Category=@Category,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("Initial=@Initial");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "OwnerId", DbType.Guid, model.OwnerId);
            db.AddInParameter(dbCommand, "UserName", DbType.AnsiString, model.UserName);
            db.AddInParameter(dbCommand, "Company", DbType.AnsiString, model.Company);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "Position", DbType.AnsiString, model.Position);
            db.AddInParameter(dbCommand, "Email", DbType.AnsiString, model.Email);
            db.AddInParameter(dbCommand, "Mobile", DbType.AnsiString, model.Mobile);
            db.AddInParameter(dbCommand, "Tel", DbType.AnsiString, model.Tel);
            db.AddInParameter(dbCommand, "Remarks", DbType.AnsiString, model.Remarks);
            db.AddInParameter(dbCommand, "Category", DbType.Int32, model.Category);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "Initial", DbType.AnsiString, model.Initial);
            db.ExecuteNonQuery(dbCommand);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Hr_Contacts ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_Contacts GetModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,OwnerId,UserName,Company,Depart,Position,Email,Mobile,Tel,Remarks,Category,UpdateTime,Initial from Hr_Contacts ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            Chenxin.Lib.Hr.Entity.Hr_Contacts model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,OwnerId,UserName,Company,Depart,Position,Email,Mobile,Tel,Remarks,Category,UpdateTime,Initial ");
            strSql.Append(" FROM Hr_Contacts ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Hr.Entity.Hr_Contacts> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,OwnerId,UserName,Company,Depart,Position,Email,Mobile,Tel,Remarks,Category,UpdateTime,Initial ");
            strSql.Append(" FROM Hr_Contacts ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Hr.Entity.Hr_Contacts> list = new List<Chenxin.Lib.Hr.Entity.Hr_Contacts>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Main_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Hr.Entity.Hr_Contacts ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Hr.Entity.Hr_Contacts model = new Chenxin.Lib.Hr.Entity.Hr_Contacts();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["OwnerId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.OwnerId = new Guid(ojb.ToString());
            }
            model.UserName = dataReader["UserName"].ToString();
            model.Company = dataReader["Company"].ToString();
            model.Depart = dataReader["Depart"].ToString();
            model.Position = dataReader["Position"].ToString();
            model.Email = dataReader["Email"].ToString();
            model.Mobile = dataReader["Mobile"].ToString();
            model.Tel = dataReader["Tel"].ToString();
            model.Remarks = dataReader["Remarks"].ToString();
            ojb = dataReader["Category"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Category = (int)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            model.Initial = dataReader["Initial"].ToString();
            return model;
        }

        #endregion  成员方法
    }
}

